// pages/games/games.js
// 记录轮询图片数组的下标
let imgIndex = 0;

let inertvalID = null;
let ting = 1;
let jy = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
  kaishi:'开始',
  winCount: 0,
  resultMessage: '加油!',
  color:'',
  btncolor:'#409EFF',
  leftSrc: '../../imgs/bu.png',
  leftImgName: 'bu',
   rightSrc: '../../imgs/wenhao.png',
   rightImgName: '',
   imgArray:[
     {
       imgsrc: '../../imgs/bu.png',
       imgname: 'bu'
     },
     {
      imgsrc: '../../imgs/jiandao.png',
      imgname: 'jiandao'
     },
     {
      imgsrc: '../../imgs/shitou.png',
      imgname: 'shitou'
     }
    ]
  },

  clickbtn(){
   /*  console.log('读取data 属性中 vanme:',this.data.vname);
       this.setData({vname: '王五'}); */
   if(ting){
    inertvalID = setInterval(this.changeimg,100);
    this.setData({kaishi:'暂停',btncolor:'red',resultMessage: 'VS',color:'black',rightSrc:'../../imgs/wenhao.png'})
    ting = 0;
    jy = 1;

   }else{
    clearInterval(inertvalID);
    this.setData({kaishi:'开始',btncolor:'#409EFF',rightSrc:'../../imgs/wenhao.png'})
    ting = 1;
    jy = 0;
    
   }
    
     
  },

  /* 
  轮询图片
  */
  changeimg(){

    imgIndex ++;

    if(imgIndex >= 3){
      imgIndex = 0;
    }
    
    let imgObject = this.data.imgArray[imgIndex];
    this.setData({leftSrc:imgObject.imgsrc,leftImgName:imgObject.imgname});
  },

  /* 
  点击选中的图片
  */
  choiceImg(e){
    if(jy){
    
    this.setData({rightSrc:e.target.dataset.src, rightImgName:e.target.dataset.name});

    if(inertvalID){
      clearInterval(inertvalID);
      inertvalID = null;
      ting = 1;
      jy = 0;
      this.setData({kaishi:'开始',btncolor:'#409EFF'})

    }

    /* 
    判断左右两边的图片
    */
   console.log(ting)
  

    if(this.data.imgArray[imgIndex].imgname == 'bu' && e.target.dataset.name == 'jiandao'){
      this.setData({resultMessage: 'YOU ARE WINNER!',winCount:this.data.winCount+1,color:'red'});
      

    }else if(this.data.imgArray[imgIndex].imgname == 'jiandao' && e.target.dataset.name == 'shitou'){
      this.setData({resultMessage: 'YOU ARE WINNER!',winCount:this.data.winCount+1,color:'red'});
      

    }else if(this.data.imgArray[imgIndex].imgname == 'shitou' && e.target.dataset.name == 'bu'){
      this.setData({resultMessage: 'YOU ARE WINNER!',winCount:this.data.winCount+1,color:'red'});
      

    }else if(this.data.imgArray[imgIndex].imgname === e.target.dataset.name){
      this.setData({resultMessage: 'YOU ARE PLAIN!',color:'green'});
     

    }else{
      this.setData({resultMessage: 'YOU ARE LOSER!',color:'yellow'});
      

    }
  }
  
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})